using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace AiProjectCSharp.Config
{
    /// <summary>
    /// 检测配置管理器
    /// </summary>
    public class DetectConfigManager
    {
        private static readonly string ConfigFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "detect_config.json");
        private static DetectConfigManager _instance;
        private Dictionary<string, string> _detectTypes = new Dictionary<string, string>();

        /// <summary>
        /// 单例实例
        /// </summary>
        public static DetectConfigManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new DetectConfigManager();
                    _instance.Initialize();
                }
                return _instance;
            }
        }

        /// <summary>
        /// 检测类型字典
        /// </summary>
        public Dictionary<string, string> DetectTypes
        {
            get { return new Dictionary<string, string>(_detectTypes); }
        }

        /// <summary>
        /// 初始化配置
        /// </summary>
        private void Initialize()
        {
            if (File.Exists(ConfigFilePath))
            {
                try
                {
                    string jsonContent = File.ReadAllText(ConfigFilePath);
                    var config = JsonConvert.DeserializeObject<DetectConfig>(jsonContent);
                    if (config != null && config.detect_static_enum_dict != null)
                    {
                        _detectTypes = config.detect_static_enum_dict;
                    }
                    else
                    {
                        LoadDefaultConfig();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"加载配置文件失败: {ex.Message}");
                    LoadDefaultConfig();
                }
            }
            else
            {
                LoadDefaultConfig();
                SaveConfig();
            }
        }

        /// <summary>
        /// 加载默认配置
        /// </summary>
        private void LoadDefaultConfig()
        {
            _detectTypes = new Dictionary<string, string>
            {
                { "001fire", "001火灾检测" },
                { "002garbage", "002垃圾检测" },
                { "003faceDetect", "003人脸检测" },
                { "004callPhone", "004打电话识别" },
                { "005fight", "005人员打架" },
                { "006safeHat", "006安全帽识别" },
                { "007smoke", "007吸烟识别" },
                { "008walkerMan", "008行人识别" },
                { "009maskDetect", "009口罩检测" },
                { "010fallDetect", "010人员跌倒识别" },
                { "011crossFenceDetect", "011翻越围栏识别" },
                { "012streetVendorDetect", "012街头流动商贩识别" }
            };
        }

        /// <summary>
        /// 保存配置到文件
        /// </summary>
        private void SaveConfig()
        {
            try
            {
                var config = new DetectConfig { detect_static_enum_dict = _detectTypes };
                string jsonContent = JsonConvert.SerializeObject(config, Formatting.Indented);
                File.WriteAllText(ConfigFilePath, jsonContent);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"保存配置文件失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 重新加载配置
        /// </summary>
        public void ReloadConfig()
        {
            Initialize();
        }

        /// <summary>
        /// 配置文件数据结构
        /// </summary>
        private class DetectConfig
        {
            public Dictionary<string, string> detect_static_enum_dict { get; set; }
        }
    }
}